#!/usr/bin/env python2.4
# 
# Copyright (c) 2010 Ping Yeh, http://pingyeh.net/.
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import datetime 
import unittest
from urange import urange

class TestUrange(unittest.TestCase):

  def check(self, results, ur):
    '''Utility function to check results from a urange iterator.

    Args:
      results: a list of results to check against urange's output.
      ur: the urange iterator.
    '''
    i = 0
    for item in ur:
      self.assertEquals(results[i], item)
      i += 1
    self.assertEquals(len(results), i)

  def testSingleIntegerArgument(self):
    self.check(range(10), urange(10))

  def testTwoIntegerArguments(self):
    self.check(range(2,10), urange(2,10))

  def testFloatArgument(self):
    self.check(range(4), urange(3.5))

  def testTwoFloatArguments(self):
    self.check([x+0.5 for x in range(2,10)],
               urange(2.5,9.6))

  def testThreeFloatArguments(self):
    self.check([x+0.5 for x in range(9,2,-1)],
               urange(9.5,3.3,-1))

  def testOneDateArgumentRaises(self):
    ur = urange(datetime.date(2010,6,7))
    self.assertRaises(TypeError, ur.next)

  def testTwoDateArguments(self):
    self.check([datetime.date(2010,6,i) for i in range(1,7)],
               urange(datetime.date(2010,6,1), datetime.date(2010,6,7)))

  def testTimeArgumentsRaises(self):
    ur = urange(datetime.time(10,1,0), 
                datetime.time(10,20,0),
                datetime.timedelta(minutes=2))
    self.assertRaises(TypeError, ur.next) # because time.__sub__(time) is not defined

  def testDatetimeArguments(self):
    self.check([datetime.datetime(2010,6,1,10,i,0) for i in range(1,20,2)],
               urange(datetime.datetime(2010,6,1,10,1,0),
                      datetime.datetime(2010,6,1,10,20,0),
                      datetime.timedelta(minutes=2)))

  def createC1(self):
    '''Utility function to define self.C1, a simple data class.'''
    class C1Simple(object):
      def __init__(self, x): self.x = x
      def __sub__(self, other): return self.x - other.x
      def __add__(self, delta): return C1Simple(self.x+delta)
      def __eq__(self, other): return self.x == other.x
      def __gt__(self, other): return self.x > other.x
      def __lt__(self, other): return self.x < other.x
    self.C1 = C1Simple

  def testSimpleObject(self):
    self.createC1()
    # test increment 
    self.check([self.C1(i) for i in range(3,10)],
               urange(self.C1(3), self.C1(10)))
    # test decrement 
    self.check([self.C1(i) for i in range(10,3,-1)],
               urange(self.C1(10), self.C1(3), -1))

  def testClassWithoutSubRaises(self):
    self.createC1()
    del self.C1.__sub__
    ur = urange(self.C1(3), self.C1(10))
    self.assertRaises(TypeError, ur.next)

  def testClassWithoutAddRaises(self):
    self.createC1()
    del self.C1.__add__
    ur = urange(self.C1(3), self.C1(10))
    self.assertRaises(TypeError, ur.next)

  def testClassWithoutLtRaises(self):
    self.createC1()
    del self.C1.__lt__
    ur = urange(self.C1(3), self.C1(10))
    self.assertRaises(TypeError, ur.next)

  def testClassWithoutGtRaises(self):
    self.createC1()
    del self.C1.__gt__
    ur = urange(self.C1(10), self.C1(3), -1)
    self.assertRaises(TypeError, ur.next)

  def testClassWithoutGtWorksForIncr(self):
    self.createC1()
    del self.C1.__gt__
    self.check([self.C1(i) for i in range(3,10)],
               urange(self.C1(3), self.C1(10)))

  def createC2AndDelta(self):
    '''Utility function to define self.C2 and self.Delta.

    self.C2 is a data class with __sub__() producing a self.Delta object.
    '''
    class Delta(object):
      def __init__(self, dx): self.dx = dx
      def __gt__(self, other): return self.dx > other.dx
      def __lt__(self, other): return self.dx < other.dx
    class C2(object):
      def __init__(self, x): self.x = x
      def __sub__(self, other): return Delta(self.x - other.x)
      def __add__(self, delta): return C2(self.x+delta.dx)
      def __eq__(self, other): return self.x == other.x
      def __gt__(self, other): return self.x > other.x
      def __lt__(self, other): return self.x < other.x
    self.C2 = C2
    self.Delta = Delta

  def testDeltaObject(self):
    self.createC2AndDelta()
    # test increment
    self.check([self.C2(i) for i in range(3,10)],
               urange(self.C2(3), self.C2(10)))
    # test decrement
    self.check([self.C2(i) for i in range(10,3,-1)],
               urange(self.C2(10), self.C2(3), self.Delta(-1)))

  def testDeltaWithoutGtRaises(self):
    self.createC2AndDelta()
    del self.Delta.__gt__
    ur = urange(self.C2(3), self.C2(10), self.Delta(1))
    self.assertRaises(TypeError, ur.next)

  def testDeltaWithoutLtRaises(self):
    self.createC2AndDelta()
    del self.Delta.__lt__
    ur = urange(self.C2(10), self.C2(3), self.Delta(-1))
    self.assertRaises(TypeError, ur.next)

  def testDeltaWithCmpWorks(self):
    self.createC2AndDelta()
    del self.Delta.__gt__
    del self.Delta.__lt__
    self.Delta.__cmp__ = lambda s, o: s.dx - o.dx
    self.check([self.C2(i) for i in range(3,10)],
               urange(self.C2(3), self.C2(10)))

if __name__ == '__main__':
  unittest.main()
